package jinyilw.swing.visuals.Rcomp;

import jinyilw.common.Operator;
import jinyilw.swing.ComponentTools;
import jinyilw.swing.visuals.Icomp.IRichLabel;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author Jinyi
 */
public class RMenuLabel extends IRichLabel// 菜单标签
{
	private static final long serialVersionUID = 8531824112488923592L;
	private static final AlphaComposite alphaComposite = ComponentTools.alphaComposite;
	private float alpha = 1f;

	public RMenuLabel()
	{
		initGUI();
	}

	public RMenuLabel(String txt)
	{
		super(txt);
		initGUI();
	}

	private void initGUI()
	{
		addMouseListener(new OverListener());
	}

	@Override
	public void updateUI()
	{
		// setUI(new BasicLabelUI());
	}

	public void setAlpha(float alpha)
	{
		this.alpha = alpha;
		repaint();
	}

	public void setClickOperator(final Operator<?> operator)
	{
		addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				operator.run();
			}
		});
	}

	private class OverListener extends MouseAdapter
	{
		@Override
		public void mouseEntered(MouseEvent e)
		{
			setAlpha(0.9f);
		}

		@Override
		public void mouseExited(MouseEvent e)
		{
			setAlpha(1f);
		}
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = g2d.getComposite();
		g2d.setComposite(alphaComposite.derive(alpha));
		super.paintComponent(g2d);
		g2d.setComposite(composite);
	}
}
